import squery from '@feidao/web/atom/nodejs/db/sql_query';
import format from '@feidao/web/atom/nodejs/dt/format';
import { spaceid, systemid } from '../config';
import { IBaseTable } from '@feidao/web/atom/nodejs/db/interfaces';

interface Message {
	[key: string]: any;
}

interface ICommodity extends IBaseTable {
	createdate_str: string;
	createdate: number;
	editdate_str: string;
	editdate: number;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const sql = `
	SELECT
		C.companyname,
		C.brand_name,
		C.brand_no,
		C.commodityid,
		C.commodity_name,
		C.creater,
		C.description,
		C.createdate,
		C.editdate,
		C.editname,
		C.first_category_name,
		C.first_category_no,
		C.product_extended,
		C.second_category_name,
		C.second_category_no,
		C.stoptime,
		C.thumbnail,
		C.type_name,
		C.type_no,
		C.unit_name,
		C.unit_no,
		C.warranty_information
	FROM
		commodity C
	`;
	const { exec, prepare } = await squery(action_id, session_id, spaceid, systemid);
	prepare(sql);
	const [res] = await exec() as [ICommodity[]];
	// 对日期进行处理
	const data = res.map((item) => {
		item.createdate_str = format(item.createdate, 'yyyy-mm-dd hh:MM:ss');
		item.editdate_str = format(item.editdate, 'yyyy-mm-dd hh:MM:ss');
		return item;
	});
	return data;
}
